// pages/appointment/clientment/menttimes.js
const app = getApp();
var comm = require('../../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cid: 0,//访问用户id
        pname: "",//项目名称
        vname: "",//项目负责人
        datelist: [],//日期列表
        timelist: [],//时刻列表
        visituser: {},//访问的用户信息
        timeclass: [
            "tiemli overtime",
            "tiemli",
            "tiemli reserved",
            "tiemli active"
        ],
        formdata: {
            date: "",
            time: "",
            cid: 0,
            reTime: "",
            projectId: 0,
            phone: "",
            desc: ""
        }//需要提交的数据
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        var _this = this;
        _this.setData({
            "cid": options.cid,
            "pid": options.pid,
            "pname": options.pname,
            "vname": options.vname,
            "formdata.cid": options.cid,
            "formdata.projectId": options.pid
        });
        _this.GetTimeTable("");
        _this.GetVisitUser();
    },

    //获取访问用户的预约设置
    GetTimeTable: function (date) {
        var cid = this.data.cid;
        app.http.POST("/MiniCardReserva/GetReservaTimeTable", { "cid": cid, "searchDate": date }, (err, data) => {
            if (data.code == 1) {
                console.log(data.data.Date);
                //默认选中日期
                var fromdate = "";
                for (var i = 0; i < data.data.Date.length; i++) {
                    if (data.data.Date[i].Checked) {
                        fromdate = data.data.Date[i].Date;
                    }
                }
                this.setData({
                    "datelist": data.data.Date,
                    "timelist": data.data.Time,
                    "formdata.date": fromdate
                });
            } else {
                wx.showToast({
                    title: '获取数据失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    SwithDate: function (e) {
        var _this = this;
        var datelist = _this.data.datelist;
        var thisIndex = e.currentTarget.dataset.index;
        var swithDate = e.currentTarget.dataset.date;
        _this.GetTimeTable(swithDate);
        for (var i = 0; i < datelist.length; i++) {
            if (i == thisIndex) {
                datelist[i].Checked = true;
            } else {
                datelist[i].Checked = false;
            }
        }
        _this.setData({
            "datelist": datelist,
            "formdata.date": swithDate
        });
    },

    ChooseTime: function (e) {
        var _this = this;
        var dataset = e.currentTarget.dataset;
        var Time = _this.data.timelist;
        var arry = [Time.AM, Time.PM, Time.Night];
        if (arry[dataset.type][dataset.index].Status != 0 && arry[dataset.type][dataset.index] != 2) {
            return;
        }
        //将所有状态为2的时刻修改
        for (var i = 0; i < arry.length; i++) {
            for (var j = 0; j < arry[i].length; j++) {
                if (dataset.type == i && dataset.index == j) {
                    //修改选中的时段
                    var newarry = arry;
                    newarry[i][j].Status = 2;
                    _this.setData({
                        "timelist": {
                            "AM": newarry[0],
                            "PM": newarry[1],
                            "Night": newarry[2]
                        },
                        "formdata.time": dataset.time
                    });
                }
                else if (arry[i][j].Status == 2) {
                    //修改原选中的时段
                    var newarry = arry;
                    newarry[i][j].Status = 0;
                    _this.setData({
                        "timelist": {
                            "AM": newarry[0],
                            "PM": newarry[1],
                            "Night": newarry[2]
                        }
                    });
                }
            }
        }
    },
    //表单提交
    formSubmit: function (e) {
        console.log(e);
        var formvalue = e.detail.value;
        var formdata = this.data.formdata;
        if (formdata.date == "") {
            wx.showToast({
                title: '请选择日期',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (formdata.time == "") {
            wx.showToast({
                title: '请选择时间',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (formvalue.phone == "") {
            wx.showToast({
                title: '手机号不能未空',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        else {//验证手机号码
            var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(formvalue.phone)) {
                wx.showToast({
                    title: '手机号有误',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
        }
        formdata.phone = formvalue.phone;
        formdata.desc = formvalue.desc;
        formdata.reTime = comm.dateFormat(formdata.date, "yyyy-MM-dd") + " " + formdata.time;
        app.http.POST("/MiniCardReserva/Reserva", formdata, (err, data) => {
            if (data.code == 1) {
                wx.showToast({
                    title: '预约成功',
                    icon: 'none',
                    duration: 2000,
                    success: function () {
                        wx.navigateBack({
                            
                        })
                    }
                })
            } else {
                wx.showToast({
                    title: '预约失败',
                    icon: 'none',
                    duration: 2000,
                    success: function () {

                    }
                })
            }
        });
    },
    GetVisitUser: function () {
        var cid = this.data.cid;
        app.http.POST("/MiniCardUser/GetVisitUserInfo", { "cid": cid }, (err, data) => {
            if (data.code == 1) {
                this.setData({
                    "visituser": data.data
                })
                wx.hideLoading()
            } else {
                wx.showToast({
                    title: '访问用户信息获取失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: this.data.visituser.NickName + "的美名片",
            path: "/pages/index/index?cid=" + this.data.cid,
            success: function (res) {
                wx.showToast({
                    title: '分享成功',
                    icon: "success",
                    duration: 200
                })
            }
        }
    }
})